package com.nulldev.util.net.packets;

import java.net.DatagramPacket;

import com.nulldev.util.net.IPUtil;
import com.nulldev.util.net.experimental.udp2.Connection;
import com.nulldev.util.net.tcp.client.TCPClient.ClientConnection;
import com.nulldev.util.net.tcp.server.TCPServer.ServerConnection;
import com.nulldev.util.net.udp.UDPServer;

public class NetInfoAdapter {

	public static NetInfo create(final String ip, final int port, final Object source) {
		return new NetInfo(ip, port, source);
	}
	
	public static NetInfo create(final Object source) {
		if (source instanceof ClientConnection) {
			final ClientConnection cc = (ClientConnection) source;
			return new NetInfo(cc.getIP(), cc.getPort(), cc);
		}else if (source instanceof ServerConnection) {
			final ServerConnection cc = (ServerConnection) source;
			return new NetInfo(cc.getIP(), cc.getPort(), cc);			
		}else if (source instanceof UDPServer) {
			final UDPServer ucs = (UDPServer) source;
			return new NetInfo(ucs.getIP(), ucs.getPort(), ucs);
		}else if (source instanceof Connection) {
			final Connection conn = (Connection) source;
			return new NetInfo(conn.getAddress().getHostAddress(), conn.getPort(), conn);
		}
		return null;
	}

	public static NetInfo generic() {
		return new NetInfo("127.0.0.1", IPUtil.nextFreePort(), null);
	}
}
